﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace eHouse
{
    public partial class ScheduleLights : PhoneApplicationPage
    {
        public System.Collections.ObjectModel.ObservableCollection<ItemViewModel> CurrentItemsForRule { get; set; }

        public ScheduleLights()
        {
            CurrentItemsForRule = new System.Collections.ObjectModel.ObservableCollection<ItemViewModel>();
            foreach (ItemViewModel i in App.ViewModel.LightItems)
            {
                CurrentItemsForRule.Add(new ItemViewModel()
                {
                    LineOne = i.LineOne,
                    SwitchState = false.ToString()
                });
            }

            DataContext = this;

            InitializeComponent();
        }

        private void ToggleSwitch_Loaded(object sender, RoutedEventArgs e)
        {
            if ((bool)((ToggleSwitch)sender).IsChecked)
            {
                ((ToggleSwitch)sender).Content = "Yes";
            }
            else
            {
                ((ToggleSwitch)sender).Content = "No";
            }
        }

        private void cancel_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.GoBack();
        }

        private void save_Click(object sender, RoutedEventArgs e)
        {
            Dictionary<string, bool> dict = new Dictionary<string, bool>();
            dict.Add("VALUE", (bool)switchStates.IsChecked);
            bool anyTrue = false;
            foreach (ItemViewModel item in CurrentItemsForRule)
            {
                anyTrue |= item.SwitchState.ToUpper() == "TRUE";
                dict.Add(item.LineOne, item.SwitchState.ToUpper() == "TRUE");
            }

            if (!anyTrue)
            {
                MessageBox.Show("You must select some room to turn the light " + switchStates.Content.ToString().ToUpper());
                return;
            }

            Rule rule = new Rule();
            rule.Id = Guid.NewGuid().ToString();
            rule.Time = time.ValueString;
            rule.Data = dict;
            App.RuleForSaving = rule;
            App.RuleAdderCall = App.ConnectionManager.AddLightRule;

            NavigationService.GoBack();
        }
    }
}